﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;
using System.IO;

namespace DELVE_GAME
{
    class Leaderboard
    {
        //make everything static
        public static List<PlayerProfile> getList(String gametype, List<PlayerProfile>list){
            List<PlayerProfile> sorted = new List<PlayerProfile>();
            List<double> temp = new List<double>();
            List<PlayerProfile> finalList = new List<PlayerProfile>();

            int gt = 0;
            if (gametype == "territories") gt = 1;
            else if (gametype == "quickwin") gt = 2;
            else if (gametype == "terrRace") gt = 3;

            foreach (PlayerProfile pp in list)
            {
                //find weighted scores in list for gametype
                temp.Add(weightedScore(pp.wins[gt], pp.games[gt]));
            }
            while (sorted.Count < list.Count)
            {
                double max = 0;
                int index = 0;
                for (int i = 0; i < temp.Count; i++)
                {
                    //finds the maximum value in temp
                    if (max < temp.ElementAt(i))
                    {
                        max = temp.ElementAt(i);
                        index = i;
                    }
                }
                sorted.Add(list.ElementAt(temp.IndexOf(max)));
                temp.Remove(max);
                temp.Insert(index, -1);
            }
            if (list.Count > 10)
            {
                for (int i = 0; i < 10; i++)
                {
                    finalList.Insert(i, sorted.ElementAt(i));
                }
            }
            else finalList = sorted;

            return finalList;
         }

         private static double weightedScore(int wins, int games)
         {
             if (games > 0)
             {
                 return (double)wins * (double)wins / (double)games;
             }
             //if total is 0, then haven't played any games yet so gets lowest ranking
             else return 0;
         }
    }
}